package com.sci.web.util;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;



/**
 * 
 * @作者 乾之轩
 * @功能 String工具类
 * 
 */

public class StringUtil extends StringUtils {

	public static final String DATE_MORMAT = "yyyy-MM-dd";
	public static final String DATE_MORMAT1 = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_MORMAT2 = "MM-dd";
	public static final String DATE_MORMAT3 = "MM-dd HH:mm";
	public static final String DATE_MORMAT4 = "yyyy\u5E74MM\u6708dd\u65E5";
	public static final String DATE_MORMAT5 = "yyyy/MM/dd";
	public static final String DATE_MORMAT6 = "EEE MMM dd HH:mm:ss Z yyyy";
	private static final String FIVE_DIGIT_NUMBER = "############0.00000";
	private static final String TWO_DIGIT_NUMBER = "############0.00";
	public static DecimalFormat decimalformat = new DecimalFormat(TWO_DIGIT_NUMBER);// 转化格式对象

	private static final double MAXIMUM_NUMBER = 9999999999d;

	private static final String CN_ZERO = "零";
	private static final String CN_SYMBOL = "￥:";
	private static final String CN_DOLLAR = "圆";
	private static final String CN_INTEGER = "整";

	private static final String[] digits = new String[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
	private static final String[] radices = new String[] { "", "拾", "佰", "仟" };
	private static final String[] bigRadices = new String[] { "", "万", "亿", "万" };
	private static final String[] decimals = new String[] { "角", "分" };

	public StringUtil() {
	}
	

	/**
	 * @功能 处理乱码
	 * @param valString
	 * @return
	 */
	public static String getGBKString(String valString) {
		String string = valString != null ? valString : "";
		String string2 = "";
		try {
			string2 = new String(string.getBytes("ISO-8859-1"), "GBK");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return string2;
	}

	public static String escapeSql(String str) {
		if (str == null) {
			return null;
		}
		return StringUtils.replace(str, "'", "''");
	}

	/**
	 * @功能 处理乱码
	 * @param valString
	 * @return
	 */
	public static String getGBKString1(String valString) {
		String string = valString != null ? valString : "";
		String string2 = "";
		try {
			string2 = new String(string.getBytes("utf-8"), "GBK");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return string2;
	}

	public static String GetFiveBrno(String brno) {
		int length = brno.length();
		if (length < 5 && isNotEmpty(brno)) {
			for (int i = 0; i < 5 - length; i++) {
				brno = brno + "0";
			}
		} else {
			brno = brno.substring(0, 5);
		}
		return brno;
	}

	/**
	 * @功能 处理null 是null 返回""
	 * @param string
	 * @return
	 */
	public static String KillNull(String string) {
		return string != null ? string : "";
	}

	public static String KillNullObj(Object object) {
		return object != null ? (String) object : "";
	}

	/**
	 * 
	 * @名称 KillNull
	 * @功能 处理空字符串
	 * @作者 乾之轩
	 * @时间 Mar 21, 2011 6:25:07 PM
	 * @param string需要处理的字符串
	 * @param defstr字符串为空时返回的值
	 * @return
	 * 
	 */
	public static String KillNull(String string, String defstr) {
		return string != null && !"".equals(string) ? string : defstr;
	}

	public static String KillEmptyAndNull(String str, String defStr) {
		return isEmpty(str) || null == str ? defStr : str;
	}

	/**
	 * @功能 获得拆分后的左半部分字符串
	 * @param string 被拆分的字符串
	 * @param splitstr 拆分字符串的标志
	 * @return
	 */
	public static String getLeftStr(String string, String splitstr) {
		return string.split(splitstr)[0];
	}

	/**
	 * @功能 获得拆分后的右半部分字符串
	 * @param string 被拆分的字符串
	 * @param splitstr 拆分字符串的标志
	 * @return
	 */
	public static String getRightStr(String string, String splitstr) {
		return string.split(splitstr)[1];
	}

	/**
	 * @功能 获得格式化得时间
	 * @param date 要转化的时间
	 * @param formate 要转化的时间格式
	 * @return
	 */
	public static String getFormatDate(String date, String formate) {
		SimpleDateFormat myformate = new SimpleDateFormat(formate);
		SimpleDateFormat myformate1 = new SimpleDateFormat("yyyy-MM-dd");
		Date myDate = null;
		if (StringUtils.isEmpty(date))
			return "";
		try {
			return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
		} catch (Exception e) {
			try {
				myDate = myformate1.parse(date);
			} catch (ParseException e0) {
				SimpleDateFormat myformate2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					myDate = myformate2.parse(date);
				} catch (ParseException e1) {
					SimpleDateFormat myformate3 = new SimpleDateFormat("MM-dd");
					try {
						myDate = myformate3.parse(date);
					} catch (ParseException e2) {
						SimpleDateFormat myformate4 = new SimpleDateFormat("MM-dd HH:mm");
						try {
							myDate = myformate4.parse(date);
						} catch (ParseException e3) {
							SimpleDateFormat myformate5 = new SimpleDateFormat("yyyy\u5E74MM\u6708dd\u65E5");
							try {
								myDate = myformate5.parse(date);
							} catch (ParseException e4) {
								SimpleDateFormat myformate6 = new SimpleDateFormat("yyyy/MM/dd");
								try {
									myDate = myformate6.parse(date);
								} catch (ParseException e5) {
									myDate = new Date();
								}
							}
						}
					}
				}
			}
		}
		return myformate.format(myDate);
	}

	/**
	 * @功能 获得格式化得时间
	 * @param date 要转化的时间
	 * @return
	 */
	public static String getFormatDateShow(String date) {
		// SimpleDateFormat myformate1 = new SimpleDateFormat("yyyy-MM-dd");
		date = StringUtil.KillNull(date);
		if (date.length() > 8) {
			return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8) + date.substring(8);
		} else if (date.length() >= 8) {
			return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
		} else {
			return date;
		}

	}

	/**
	 * @功能 获得格式化得时间
	 * @param date 要转化的时间
	 * @param formate 要转化的时间格式
	 * @return
	 */
	public static String getFormatDate(Date date, String formate) {
		SimpleDateFormat myformate = new SimpleDateFormat(formate);
		return myformate.format(date);
	}

	/**

	/**
	 * @功能 判断对象不是空对象
	 * @param object
	 * @return true:不为空；false:为空
	 */

	public static boolean isNotEmpty(String str) {
		return str != null && str.length() > 0 && (!"undefined".equals(str));
	}

	/**
	 * @功能 判断对象是空对象
	 * @param object
	 * @return true:为空，false:不为空
	 */

	public static boolean isEmpty(String str) {
		return str == null || str.trim().length() == 0 || str.equals("null");
	}

	/**
	 * 
	 * @名称 setValues
	 * @描述 将结果集封装到bean里面
	 * @作者 liupei
	 * @时间 Nov 9, 2010 1:30:05 PM 使用注意事项 实体bean属性名称要和数据库中的一致 并查询出实体bena的所有字段
	 */
	@SuppressWarnings("rawtypes")
	public static void setValues(Object object, ResultSet rs) {
		Class myclass = object.getClass();

		Field[] fields = myclass.getDeclaredFields();
		int fieldslen = fields.length;
		for (int i = 0; i < fieldslen; ++i) {
			Field field = fields[i];
			field.setAccessible(true);
			String fieldname = field.getName();
			try {
				field.set(object, KillNull(rs.getString(fieldname)));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @名称 numberToChinese
	 * @描述 将数子转为大写（汉字）如4得到 肆元整
	 * @作者 liupei
	 * @时间 Nov 30, 2010 11:08:55 AM
	 */
	public static String numberToChinese(double moneyVal) {

		String currencyDigits = String.valueOf(moneyVal);

		if (check(currencyDigits)) {

			String integral = null; // 整数部分
			String decimal = null; // 小数部分
			String outputCharacters = null; // 最终转换输出结果

			String d = null;
			int zeroCount = 0, p = 0, quotient = 0, modulus = 0;

			// 删除数字中的逗号,
			currencyDigits = currencyDigits.replace("/,/g", "");
			// 删除数字左边的0
			currencyDigits = currencyDigits.replace("/^0+/", "");

			// 拆分数字中的整数与小数部分
			String[] parts = currencyDigits.split("\\.");
			if (parts.length > 1) {
				integral = parts[0];
				decimal = parts[1];

				// 如果小数部分长度大于2，四舍五入，保留两位小数
				if (decimal.length() > 2) {
					long dd = Math.round(Double.parseDouble("" + decimal) * 100);
					decimal = Long.toString(dd);
				}

			} else {
				integral = parts[0];
				decimal = "0";
			}

			// Start processing:
			outputCharacters = "";
			// Process integral part if it is larger than 0:
			if (Double.parseDouble(integral) > 0) {

				zeroCount = 0;

				for (int i = 0; i < integral.length(); i++) {

					p = integral.length() - i - 1;
					d = integral.substring(i, i + 1);

					quotient = p / 4;
					modulus = p % 4;
					if (d.equals("0")) {
						zeroCount++;
					} else {
						if (zeroCount > 0) {
							outputCharacters += digits[0];
						}
						zeroCount = 0;
						outputCharacters += digits[Integer.parseInt(d)] + radices[modulus];
					}
					if (modulus == 0 && zeroCount < 4) {
						outputCharacters += bigRadices[quotient];
					}
				}
				outputCharacters += CN_DOLLAR;
			}

			// Process decimal part if it is larger than 0:
			if (Double.parseDouble(decimal) > 0) {
				for (int i = 0; i < decimal.length(); i++) {
					d = decimal.substring(i, i + 1);
					if (!d.equals("0")) {
						outputCharacters += digits[Integer.parseInt(d)] + decimals[i];
					} else {
						if (i == 0) {
							outputCharacters += CN_ZERO;
						}
					}
				}
			}

			// Confirm and return the final output string:
			if (outputCharacters.equals("")) {
				outputCharacters = CN_ZERO + CN_DOLLAR;
			}
			if (decimal == null || decimal.equals("0") || decimal.equals("00")) {
				outputCharacters += CN_INTEGER;
			}

			outputCharacters = "  " + outputCharacters;
			return outputCharacters;
		} else {
			return null;
		}
	}

	/**
	 * 方法描述： 金额转换为大写
	 * 
	 * @param moneyVal
	 * @return String
	 * @author WangShaoZe
	 * @date Oct 9, 2012 10:57:19 AM
	 */
	public static String numberToChineseWithNoPrefix(double moneyVal) {

		String currencyDigits = String.valueOf(moneyVal);

		if (check(currencyDigits)) {

			String integral = null; // 整数部分
			String decimal = null; // 小数部分
			String outputCharacters = null; // 最终转换输出结果

			String d = null;
			int zeroCount = 0, p = 0, quotient = 0, modulus = 0;

			// 删除数字中的逗号,
			currencyDigits = currencyDigits.replace("/,/g", "");
			// 删除数字左边的0
			currencyDigits = currencyDigits.replace("/^0+/", "");

			// 拆分数字中的整数与小数部分
			String[] parts = currencyDigits.split("\\.");
			if (parts.length > 1) {
				integral = parts[0];
				decimal = parts[1];

				// 如果小数部分长度大于2，四舍五入，保留两位小数
				if (decimal.length() > 2) {
					long dd = Math.round(Double.parseDouble("" + decimal) * 100);
					decimal = Long.toString(dd);
				}

			} else {
				integral = parts[0];
				decimal = "0";
			}

			// Start processing:
			outputCharacters = "";
			// Process integral part if it is larger than 0:
			if (Double.parseDouble(integral) > 0) {

				zeroCount = 0;

				for (int i = 0; i < integral.length(); i++) {

					p = integral.length() - i - 1;
					d = integral.substring(i, i + 1);

					quotient = p / 4;
					modulus = p % 4;
					if (d.equals("0")) {
						zeroCount++;
					} else {
						if (zeroCount > 0) {
							outputCharacters += digits[0];
						}
						zeroCount = 0;
						outputCharacters += digits[Integer.parseInt(d)] + radices[modulus];
					}
					if (modulus == 0 && zeroCount < 4) {
						outputCharacters += bigRadices[quotient];
					}
				}
				outputCharacters += CN_DOLLAR;
			}

			// Process decimal part if it is larger than 0:
			if (Double.parseDouble(decimal) > 0) {
				for (int i = 0; i < decimal.length(); i++) {
					d = decimal.substring(i, i + 1);
					if (!d.equals("0")) {
						outputCharacters += digits[Integer.parseInt(d)] + decimals[i];
					} else {
						if (i == 0) {
							outputCharacters += CN_ZERO;
						}
					}
				}
			}

			// Confirm and return the final output string:
			if (outputCharacters.equals("")) {
				outputCharacters = CN_ZERO + CN_DOLLAR;
			}
			if (decimal == null || decimal.equals("0") || decimal.equals("00")) {
				outputCharacters += CN_INTEGER;
			}

			return outputCharacters;
		} else {
			return null;
		}
	}

	/**
	 * 检查输入数字的合法性
	 * 
	 * @param currencyDigits
	 * @return
	 */
	private static boolean check(final String currencyDigits) {
		if (currencyDigits == null || currencyDigits.trim().equals("")) {
			System.out.println("没有输入要转换的数字");
			return false;
		}

		Pattern pattern = Pattern.compile("/[^,.\\d]/", Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(currencyDigits);

		if (matcher.find()) {
			System.out.println("数字中含有非法字符!");
			return false;
		}

		pattern = Pattern.compile("/^((\\d{1,3}(,\\d{3})*(.((\\d{3},)*\\d{1,3}))?)|(\\d+(.\\d+)?))$/", Pattern.CASE_INSENSITIVE);
		matcher = pattern.matcher(currencyDigits);
		if (matcher.find()) {
			System.out.println("错误的数字格式!");
			return false;
		}

		if (Double.parseDouble(currencyDigits) > MAXIMUM_NUMBER) {
			System.out.println("超出转换最大范围!");
			return false;
		}

		return true;
	}

	/**
	 * 
	 * @名称 getHalfangle
	 * @描述 将全角字符转换为半角
	 * @作者 liupei
	 * @时间 Dec 28, 2010 9:19:43 AM
	 */
	public static String getHalfangle(String string) {
		String outStr = "";
		String Tstr = "";
		byte[] b = null;

		for (int i = 0; i < string.length(); i++) {
			try {
				Tstr = string.substring(i, i + 1);
				b = Tstr.getBytes("unicode");
			} catch (java.io.UnsupportedEncodingException e) {
				e.printStackTrace();
			}

			if (b[3] == -1) {
				b[2] = (byte) (b[2] + 32);
				b[3] = 0;
				try {
					outStr = outStr + new String(b, "unicode");
				} catch (java.io.UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			} else
				outStr = outStr + Tstr;
		}

		return outStr;
	}

	/**
	 * 
	 * @名称 isMore
	 * @功能 num1大于num2 返回 1;num1等于num2 返回 0; num1小于num2 返回 -1;
	 * @作者 乾之轩
	 * @时间 Aug 1, 2012 9:19:52 AM
	 * @param num1
	 * @param num2
	 * @return
	 * 
	 */
	public static int isMore(String num1, String num2) {
		if (isEmpty(num1)) {
			num1 = "0";
		}
		if (isEmpty(num2)) {
			num2 = "0";
		}
		BigDecimal data1 = new BigDecimal(num1);
		BigDecimal data2 = new BigDecimal(num2);
		return data1.compareTo(data2);
	}

	/**
	 * 
	 * @名称 getArrary
	 * @功能 将动态参数封装成数组
	 * @作者 乾之轩
	 * @时间 Aug 1, 2012 11:52:36 AM
	 * @param s
	 * @return
	 * 
	 */
	public static List<String> getArrary(String... s) {
		List<String> list = new ArrayList<String>();
		for (String string : s) {
			list.add(string);
		}
		return list;
	}

	/**
	 * 
	 * @名称 isContChinese
	 * @描述 判断字符串中是否包含中文字符
	 * @参数
	 * @param s 需要验证的字符串
	 * @返回值 boolean
	 * @作者 admin
	 * @时间 Apr 20, 2011 4:04:41 PM
	 */
	public static boolean isContChinese(String s) {
		boolean flag = false;
		char[] charArray = s.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			if ((charArray[i] >= 0x4e00) && (charArray[i] <= 0x9fbb)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	/**
	 * 
	 * @名称 isBigerZero
	 * @描述 判断数字字符串是否大于0，大于0返回true,小于等于0返回false
	 * @参数
	 * @param s
	 * @参数
	 * @return
	 * @返回值 boolean
	 * @作者 luanhaowei
	 * @时间 2011-8-24 下午05:41:45
	 */
	public static boolean isBigerZero(String s) {
		if (s == null || "".equals(s.trim())) {
			s = "0.00";
		}
		s = s.replaceAll(",", "");
		return isMore(s, "0.00") > 0;
	}

	/**
	 * 
	 * @名称 AddUnderlineByUppercase
	 * @描述 在大写字母前加_ 改方法不是通用方法在下一个项目中可以删除
	 * @作者 乾之轩
	 * @时间 May 13, 2011 5:23:01 PM
	 */
	public static String AddUnderlineByUppercase(String str) {
		String result = "";
		StringBuffer stringBuffer = new StringBuffer();
		if (str == null || str.length() == 0) {
			return str;
		}
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (Character.isUpperCase(c)) {
				stringBuffer.append("_").append(Character.toLowerCase(c));
			} else {
				stringBuffer.append(c);
			}
		}
		result = stringBuffer.toString();
		if (result.startsWith("_")) {
			result = result.substring(1);
		}
		return result;
	}

	/**
	 * 
	 * @名称 changeCharInString
	 * @描述 修改字符串某个位置的字符如 输入 ("abc",2,'e') 输出"abe"
	 * @作者 乾之轩
	 * @时间 May 13, 2011 5:27:30 PM
	 */
	public static String changeCharInString(String source, int position, char changeChar) throws Exception {
		if (source == null) {
			return null;
		}
		char[] sourceList = source.toCharArray();

		if (source.length() <= position) {
			throw new Exception("输入参数不合法");
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < sourceList.length; i++) {
			if (i == position) {
				sb.append(changeChar);
			} else {
				sb.append(sourceList[i]);
			}
		}

		return sb.toString();
	}

	/**
	 * 
	 * @名称 arrayToString
	 * @描述 数组转换到字符串 StringUtil.join(str, "'", ",");StringUtil.join(str, "", ",")
	 * @参数
	 * @param array 数组
	 * @参数
	 * @param marks 数组转换之后每个元素用单引号还是其他可以是空
	 * @参数
	 * @param separator 每个元素之间的分隔符
	 * @参数
	 * @return
	 * @返回值 String
	 * @作者 luanhaowei
	 * @时间 2011-8-25 下午04:40:36
	 */
	public static String arrayToString(Object[] array, String marks, String separator) {
		if (separator == null) {
			separator = "";
		}
		int arraySize = array.length;
		int bufSize = (arraySize == 0) ? 0 : (array[0].toString().length() + separator.length()) * arraySize;

		StringBuffer buf = new StringBuffer(bufSize);

		for (int i = 0; i < arraySize; ++i) {
			if (i > 0) {
				buf.append(separator);
			}
			buf.append(marks);
			buf.append(array[i]);
			buf.append(marks);
		}
		return buf.toString();
	}

	/**
	 * 
	 * @名称 subStrRight
	 * @描述 从右边截取字符串
	 * @参数
	 * @param inputStr 输入字符串
	 * @参数
	 * @param count 截取长度
	 * @参数
	 * @return 截取后的字符串
	 * @返回值 String
	 * @作者 luanhaowei
	 * @时间 2011-9-29 下午01:59:58
	 */
	public static String subStrRight(String inputStr, int count) {
		if (isEmpty(inputStr)) {
			return "";
		}
		count = (count > inputStr.length()) ? inputStr.length() : count;
		return inputStr.substring(inputStr.length() - count, inputStr.length());
	}

	/**
	 * 
	 * @名称 stringToDate
	 * @描述 (方法描述)
	 * @参数
	 * @param str
	 * @参数
	 * @return
	 * @参数
	 * @throws ParseException
	 * @返回值 Date
	 * @作者 caotieshuan
	 * 
	 * @时间 Nov 23, 2011 3:41:26 PM
	 */
	public static long getDateTime(String str) throws ParseException {
		long data = 0;
		try {
			if (str != null && !"".equals(str)) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
				System.out.println("tst1=" + sdf.parse(str).getTime());
				data = sdf.parse(str).getTime();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}

	/**
	 * 
	 * @名称 dateFormat
	 * @描述 获得符合要求的日期类型的字符串
	 * @作者 乾之轩
	 * @时间 Dec 26, 2011 6:06:16 PM
	 */
	public static String dateFormat(String date, Integer flag) {
		String formatDate = "";
		if (isEmpty(date)) {
			throw new NullPointerException("date 不能为空!");
		}
		if (date.length() != 8) {
			throw new RuntimeException("date 长度不够! ");
		}

		if (flag == null) {
			flag = 1;
		}

		if (flag == 1) {
			formatDate = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6);
		}

		return formatDate;

	}

	/**
	 * 
	 * @名称 Chines2Unicode
	 * @描述 中文转Unicode
	 * @作者 乾之轩
	 * @时间 Dec 30, 2011 11:31:14 AM
	 */
	public static String Chines2Unicode(String str) {
		char[] arChar = str.toCharArray();
		int iValue = 0;
		String uStr = "";
		for (int i = 0; i < arChar.length; i++) {
			iValue = (int) str.charAt(i);
			if (iValue <= 256) {
				uStr += "\\u00" + Integer.toHexString(iValue);
			} else {
				uStr += "\\u" + Integer.toHexString(iValue);
			}
		}
		return uStr;
	}

	/**
	 * 
	 * @名称 Unicode2Chines
	 * @描述 将Unicode转化为中文
	 * @作者 乾之轩
	 * @时间 Dec 30, 2011 11:42:10 AM
	 */
	public static String Unicode2Chines(String theString) {

		char aChar;

		int len = theString.length();

		StringBuffer outBuffer = new StringBuffer(len);

		for (int x = 0; x < len;) {

			aChar = theString.charAt(x++);

			if (aChar == '\\') {

				aChar = theString.charAt(x++);

				if (aChar == 'u') {

					// Read the xxxx

					int value = 0;

					for (int i = 0; i < 4; i++) {

						aChar = theString.charAt(x++);

						switch (aChar) {

						case '0':

						case '1':

						case '2':

						case '3':

						case '4':

						case '5':

						case '6':
						case '7':
						case '8':
						case '9':
							value = (value << 4) + aChar - '0';
							break;
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							value = (value << 4) + 10 + aChar - 'a';
							break;
						case 'A':
						case 'B':
						case 'C':
						case 'D':
						case 'E':
						case 'F':
							value = (value << 4) + 10 + aChar - 'A';
							break;
						default:
							throw new IllegalArgumentException("Malformed   \\uxxxx   encoding.");
						}

					}
					outBuffer.append((char) value);
				} else {
					if (aChar == 't')
						aChar = '\t';
					else if (aChar == 'r')
						aChar = '\r';

					else if (aChar == 'n')

						aChar = '\n';

					else if (aChar == 'f')

						aChar = '\f';

					outBuffer.append(aChar);

				}

			} else

				outBuffer.append(aChar);

		}

		return outBuffer.toString();

	}

	public static String getDecimalFormat(String str) {
		DecimalFormat fmt = new DecimalFormat("##,###,###,###,##0.00");
		String outStr = null;
		double d;
		try {
			if (str == null || "".equals(str)) {
				str = "0.00";
			}
			d = Double.parseDouble(str);
			outStr = fmt.format(d);
		} catch (Exception e) {
		}
		return outStr;
	}

	public static String getNextUpDate(String strDate, int day) {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyyMMdd");
		java.util.Date date;
		String result = "";
		try {
			date = formatter.parse(strDate);
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			calendar.add(GregorianCalendar.DATE, -1);
			date = calendar.getTime();
			System.out.println(formatter.format(date));
			result = formatter.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 
	 * 方法描述： 中文转码函数
	 * 
	 * @param str
	 * @param encode
	 * @return String
	 * @author 栾好威
	 * @date 2013-12-29 下午2:59:42
	 */
	public static String encode(String str, String encode) {
		try {
			return URLEncoder.encode(str, encode);
		} catch (UnsupportedEncodingException e) {
		}
		return str;
	}

	/**
	 * 
	 * 方法描述：
	 * 
	 * @param str
	 * @param encode
	 * @param parmArr parmArr[0]=0时表示通过URL?parm=encodeURIComponent(
	 *            encodeURIComponent
	 *            (parm))或者AJAX的data："parm="+encodeURIComponent
	 *            (encodeURIComponent(parm))这两种传值方式
	 * @param parmArr parmArr[0]=1或者不传值时时表示通过AJAX的data：{parm：encodeURIComponent(
	 *            encodeURIComponent(parm))}的这种传值方式
	 * @return String
	 * @author 栾好威
	 * @date 2013-12-26 上午11:57:54
	 */
	public static String decode(String str, String encode, String... parmArr) {
		try {
			if (parmArr != null && parmArr.length > 0 && "0".equals(parmArr[0])) {
				str = URLDecoder.decode(str, encode);
			} else if (parmArr != null && parmArr.length > 0 && "1".equals(parmArr[0])) {
				str = URLDecoder.decode(str, encode);
				str = URLDecoder.decode(str, encode);
			} else {
				str = URLDecoder.decode(str, encode);
				str = URLDecoder.decode(str, encode);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return str;
	}

	/**
	 * 
	 * 方法描述： 数组去除重复元素后重新组织元素
	 * 
	 * @param tokens数组
	 * @param separator重新组织元素分隔符
	 * @return String
	 * @author 栾好威
	 * @date 2014-1-24 上午11:44:30
	 */
	public static String deleteRepeat(Object[] tokens, String separator) {
		for (int i = 0; i < tokens.length; i++) {
			for (int j = 0; j < tokens.length; j++) {
				if (tokens[i] != null && tokens[i].equals(tokens[j]) && i != j) {
					tokens[j] = null;
				}
			}
		}
		StringBuffer resstr = new StringBuffer(50);
		for (int i = 0; i < tokens.length; i++) {
			if (tokens[i] != null) {
				resstr.append(tokens[i]).append(separator);
			}
		}
		return resstr.toString();
	}

	public static String moneyFormatHalfDown(double val, int p8043) {
		String moneyPattern = "#,##0.00";
		DecimalFormat dFormat = null;
		if (0 == p8043) {
			moneyPattern = "#,##0";
		} else if (1 == p8043) {
			moneyPattern = "#,##0.0";
		} else if (5 == p8043) {
			moneyPattern = "#,##0.00000";
		}
		BigDecimal bigDecimal = new BigDecimal("0.00");
		BigDecimal bigDecimal1 = new BigDecimal(val + "");
		BigDecimal bigDecimal2 = new BigDecimal(1 + "");
		val = bigDecimal.add(bigDecimal1).divide(bigDecimal2, 20, BigDecimal.ROUND_HALF_UP).divide(bigDecimal2, p8043, BigDecimal.ROUND_FLOOR).doubleValue();
		dFormat = new DecimalFormat(moneyPattern);
		return dFormat.format(val);
	}

	/**
	 * 
	 * 方法描述：
	 * 
	 * @param val
	 * @param p8043 小数位数,支持0,1,2,5位
	 * @param round 0-取底,1-取顶,2-四舍五入
	 * @return String
	 * @author 栾好威
	 * @date 2014-3-7 下午5:25:07
	 */
	public static String moneyFormat(double val, int p8043, int round) {
		String moneyPattern = "#,##0.00";
		DecimalFormat dFormat = null;
		if (0 == p8043) {
			moneyPattern = "#,##0";
		} else if (1 == p8043) {
			moneyPattern = "#,##0.0";
		} else if (5 == p8043) {
			moneyPattern = "#,##0.00000";
		}
		int scale = BigDecimal.ROUND_HALF_UP;
		if (0 == round) {
			scale = BigDecimal.ROUND_FLOOR;

		} else if (1 == round) {
			scale = BigDecimal.ROUND_CEILING;
		}
		BigDecimal bigDecimal = new BigDecimal("0.00");
		BigDecimal bigDecimal1 = new BigDecimal(val + "");
		BigDecimal bigDecimal2 = new BigDecimal(1 + "");
		val = bigDecimal.add(bigDecimal1).divide(bigDecimal2, 20, BigDecimal.ROUND_HALF_UP).divide(bigDecimal2, p8043, scale).doubleValue();
		dFormat = new DecimalFormat(moneyPattern);
		return dFormat.format(val);
	}

	public static String getFileName(String filePathName) {
		filePathName = filePathName.replace("\\", "/");
		String[] fileArr = filePathName.split("/");
		filePathName = fileArr[fileArr.length - 1];
		return filePathName;
	}

	// 方法描述：数字转换大写(同上面的numberToChineseWithNoPrefix方法，上面的方法中转换成double类型时，可能会出现科学计数法，会出现错误)
	public static String numberToChineseWithNoPrefix(String currencyDigits) {
		if (check(currencyDigits)) {

			String integral = null; // 整数部分
			String decimal = null; // 小数部分
			String outputCharacters = null; // 最终转换输出结果

			String d = null;
			int zeroCount = 0, p = 0, quotient = 0, modulus = 0;

			// 删除数字中的逗号,
			currencyDigits = currencyDigits.replace("/,/g", "");
			// 删除数字左边的0
			currencyDigits = currencyDigits.replace("/^0+/", "");

			// 拆分数字中的整数与小数部分
			String[] parts = currencyDigits.split("\\.");
			if (parts.length > 1) {
				integral = parts[0];
				decimal = parts[1];

				// 如果小数部分长度大于2，四舍五入，保留两位小数
				if (decimal.length() > 2) {
					long dd = Math.round(Double.parseDouble("" + decimal) * 100);
					decimal = Long.toString(dd);
				}

			} else {
				integral = parts[0];
				decimal = "0";
			}

			// Start processing:
			outputCharacters = "";
			// Process integral part if it is larger than 0:
			if (Double.parseDouble(integral) > 0) {

				zeroCount = 0;

				for (int i = 0; i < integral.length(); i++) {

					p = integral.length() - i - 1;
					d = integral.substring(i, i + 1);

					quotient = p / 4;
					modulus = p % 4;
					if (d.equals("0")) {
						zeroCount++;
					} else {
						if (zeroCount > 0) {
							outputCharacters += digits[0];
						}
						zeroCount = 0;
						outputCharacters += digits[Integer.parseInt(d)] + radices[modulus];
					}
					if (modulus == 0 && zeroCount < 4) {
						outputCharacters += bigRadices[quotient];
					}
				}
				outputCharacters += CN_DOLLAR;
			}

			// Process decimal part if it is larger than 0:
			if (Double.parseDouble(decimal) > 0) {
				for (int i = 0; i < decimal.length(); i++) {
					d = decimal.substring(i, i + 1);
					if (!d.equals("0")) {
						outputCharacters += digits[Integer.parseInt(d)] + decimals[i];
					} else {
						if (i == 0) {
							outputCharacters += CN_ZERO;
						}
					}
				}
			}

			// Confirm and return the final output string:
			if (outputCharacters.equals("")) {
				outputCharacters = CN_ZERO + CN_DOLLAR;
			}
			if (decimal == null || decimal.equals("0") || decimal.equals("00")) {
				outputCharacters += CN_INTEGER;
			}

			// outputCharacters = CN_SYMBOL + outputCharacters;
			return outputCharacters;
		} else {
			return null;
		}
	}

	/**
	 * 
	 * 方法描述： 验证字符串是否纯数字
	 * 
	 * @param str
	 * @return boolean
	 * @author 栾好威
	 * @date 2014-6-14 上午9:14:45
	 */
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (isNum.matches()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * 方法描述：验证企业组织机构代码
	 * 
	 * @param code
	 * @return boolean
	 * @author 栾好威
	 * @date 2014-6-14 上午9:17:11
	 */
	public static boolean isValidEntpCode(String code) {
		int[] ws = { 3, 7, 9, 10, 5, 8, 4, 2 };
		String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String regex = "^([0-9A-Z]){8}-[0-9|X]$";

		if (!code.matches(regex)) {
			return false;
		}
		int sum = 0;
		for (int i = 0; i < 8; i++) {
			sum += str.indexOf(String.valueOf(code.charAt(i))) * ws[i];
		}

		int c9 = 11 - (sum % 11);

		String sc9 = String.valueOf(c9);
		if (11 == c9) {
			sc9 = "0";
		} else if (10 == c9) {
			sc9 = "X";
		}
		return sc9.equals(String.valueOf(code.charAt(9)));
	}

	/**
	 * 
	 * 方法描述:把字符串1@2@3@4转换成'1','2','3','4'
	 * 
	 * @param result
	 * @param unit
	 * @return String
	 * @author 李伟
	 * @date 2014-3-20 下午03:41:55
	 */
	public static String converUnit(String result, String unit1, String util2) {
		if (isEmpty(result)) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		String results[] = result.split(unit1);
		for (String s : results) {
			sb.append(util2);
			sb.append(s);
			sb.append(util2);
			sb.append(",");
		}
		result = sb.toString();
		if (result.length() > 0) {
			return result.substring(0, result.length() - 1);
		}
		return "";
	}

	public static void main(String[] args) {
	}
	public static String numberFormat(String s, int precision) {
		Double d = Double.parseDouble(s);
		// 1$代表是从第几位开始格式化,","每3位数字之间用"，"分隔,".2"代表小数点后面取2位."f"代表是浮点类型
		return numberFormat(d, precision);
	}
	public static String numberFormat(Double d, int precision) {
		// %1$,.2f
		// 1$代表是从第几位开始格式化,","每3位数字之间用"，"分隔,".2"代表小数点后面取2位."f"代表是浮点类型
		String format = "%1$,." + precision + "f";
		return (String.format(format, d));
	}
	
	
}
